// components/play/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isAll:Boolean,
      dataArr:{
        type: Array,
        observer: function (newVal, oldVal) {
        //  console.log(newVal);
          this.data.current = true
          if (this.data.current) {
            this.init();
            this.data.current = true
          }
        }
      },
      allXuan:{
        type: Boolean,  
        observer: function (newVal, oldVal) {  
        //  console.log(newVal);
          if(this.data.current){
            this.init();
            this.data.current=true
          }  
        }
      }
  },

  /**
   * 组件的初始数据
   */
  data: {
    current:true
  },
  attached:function(){
    this.init();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    xuan:function(){
      let newArr = this.data.dataArr.filter(val=>{
        return val.xuan
      })
      this.triggerEvent('taps', {
        data:{isAll:this.data.allXuan,isXuan:newArr}
      }, {}) 
    },
    init:function(){
      this.data.dataArr.forEach((val) => {
          val.xuan = this.data.allXuan;
      })
      this.setData({
        dataArr: this.data.dataArr
      })
    },
    onAllXuan:function(){
      let isXuan=this.data.allXuan;
      this.data.dataArr.forEach((val) => { val.xuan = !isXuan });
      this.setData({
        dataArr: this.data.dataArr,
        allXuan: !isXuan
      })
      this.data.current=true;
      this.triggerEvent('vXuan', {
       xuan : this.data.dataArr
      }, {})  
    },
    onXuan:function(e){
      var index=e.currentTarget.dataset.index;
      var isXuan = this.data.dataArr[index].xuan;
      if(isXuan){
        this.data.dataArr[index].xuan=false
      }else{
        this.data.dataArr[index].xuan = true;
      }
     if(this.data.allXuan){
       this.data.current = false;
     }
      this.setData({
        dataArr:this.data.dataArr,
        allXuan:this.data.dataArr.every((val) => val.xuan),
      })
    }
  }
})
